package ysq.xyg.web.controller;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.model.po.*;
import ysq.xyg.web.model.vo.WebArticleVo;
import ysq.xyg.web.model.vo.WebBannerVo;
import ysq.xyg.web.model.vo.WebNavigationVo;
import ysq.xyg.web.model.vo.WebProductVo;
import ysq.xyg.web.service.*;
import ysq.xyg.web.util.WebBaseContent;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 网站pc端
 *
 * @author yu sheng qiang
 * @create 2017-03-22 11:57
 **/
@RestController
public class PcController {
    private static Logger log= LoggerFactory.getLogger(PcController.class);
    @Resource
    private WebProductService webProductService;
    @Resource
    private WebNavigationService webNavigationService;
    @Resource
    private WebTagTypeService webTagTypeService;
    @Resource
    private WebColumnTypeService webColumnTypeService;
    @Resource
    private WebBannerService webBannerService;
    @Resource
    private WebConfigService webConfigService;
    @Resource
    private WebArticleService webArticleService;
    /**
     * 统一首页
     *
     * @param confId 系统配置id
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/")
    public ModelAndView index(HttpServletRequest request,String confId,String navId){
        WebConfig webConfig=acceptWebConfig(confId);
        List<WebNavigation> webNavigationList=webNavigationService.acceptList(new WebNavigationVo(BaseConstant.base_use_type_pc,webConfig.getUserId()));
        ModelAndView modelAndView=new ModelAndView("/pc/"+webConfig.getSiteStyle()+"/index");
        modelAndView.addObject("webConfig",webConfig);
        modelAndView.addObject("webNavigationList",webNavigationList);
        if(StrUtil.isEmpty(navId)){
            if(webNavigationList!=null&&webNavigationList.size()>0){
                modelAndView.addObject("navId",webNavigationList.get(0).getId());
            }
        }else{
            modelAndView.addObject("navId",navId);
        }


        return modelAndView;
    }
    /**
     * 统一跳转自定义页面
     *
     * @param confId 系统配置id
     * @param fileName 文件名称
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/pages")
    public ModelAndView pages(String confId,String fileName,String navId){
        WebConfig webConfig=acceptWebConfig(confId);
        List<WebNavigation> webNavigationList=webNavigationService.acceptList(new WebNavigationVo(BaseConstant.base_use_type_pc,webConfig.getUserId()));
        ModelAndView modelAndView=new ModelAndView("/pc/"+webConfig.getSiteStyle()+"/"+fileName);
        modelAndView.addObject("webConfig",webConfig);
        if(StrUtil.isEmpty(navId)){
            if(webNavigationList!=null&&webNavigationList.size()>0){
                modelAndView.addObject("navId",webNavigationList.get(0).getId());
            }
        }else{
            modelAndView.addObject("navId",navId);
        }

        modelAndView.addObject("webNavigationList",webNavigationList);
        return modelAndView;
    }
    /**
     * 统一跳转自定义页面
     *
     * @param confId 系统配置id
     * @param fileName 文件名称
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/page/{confId}")
    public ModelAndView page(@PathVariable String confId,String fileName){
        WebConfig webConfig=acceptWebConfig(confId);
        ModelAndView modelAndView=new ModelAndView("/pc/"+webConfig.getSiteStyle()+"/"+fileName);
        modelAndView.addObject("webConfig",webConfig);
        return modelAndView;
    }
    /**
     * 统一跳转商品页面
     *
     * @param confId 系统配置id
     * @param productId 商品id
     * @param fileName 文件名称
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/product/{confId}")
    public ModelAndView page(@PathVariable String confId,String productId,String fileName){
        WebConfig webConfig=acceptWebConfig(confId);
        ModelAndView modelAndView=new ModelAndView("/pc/"+webConfig.getSiteStyle()+"/"+fileName);
        modelAndView.addObject("webConfig",webConfig);
        modelAndView.addObject("product",webProductService.selectByPrimaryKey(productId));
        return modelAndView;
    }
    /**
     * 统一文档页面
     *
     * @param confId  配置id
     * @param id 文章标识
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/article/{confId}")
    public ModelAndView articleDesc(@PathVariable String confId, String id, String navId){
        WebConfig webConfig=acceptWebConfig(confId);
        List<WebNavigation> webNavigationList=webNavigationService.acceptList(new WebNavigationVo(BaseConstant.base_use_type_pc,webConfig.getUserId()));
        ModelAndView modelAndView=new ModelAndView("/pc/"+webConfig.getSiteStyle()+"/articleDesc");
        if(StrUtil.isEmpty(navId)){
            if(webNavigationList!=null&&webNavigationList.size()>0){
                modelAndView.addObject("navId",webNavigationList.get(0).getId());
            }
        }else{
            modelAndView.addObject("navId",navId);
        }
        modelAndView.addObject("webNavigationList",webNavigationList);
        modelAndView.addObject("webConfig",webConfig);
        modelAndView.addObject("article",webArticleService.selectByPrimaryKey(id));
        return modelAndView;
    }
    /**
     * 统一关于我们页面
     *
     * @param siteSyple 网站风格
     * @param confId 系统配置id
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/about/{siteSyple}")
    public ModelAndView about(@PathVariable String siteSyple, String confId){
        ModelAndView modelAndView=new ModelAndView("/pc/"+siteSyple+"/about");
        modelAndView.addObject("webConfig",acceptWebConfig(confId));
        return modelAndView;
    }
    /**
     * 通过标签获取对应文章目录和目录下文章
     *
     * @param tagId 标签id
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/acceptColumnTypeAndArticle")
    public ReturnEntity acceptColumnTypeAndArticle(String tagId,int num){

        List<WebColumnType> list=webColumnTypeService.listLeftByTagId(tagId);
        if(list!=null&&list.size()>0){
           for(WebColumnType webColumnType:list){
               List<WebArticle> webArticleList=webArticleService.acceptList(new WebArticleVo(webColumnType.getId()));
               if(webArticleList!=null&&num>0&&webArticleList.size()>num){
                   List<WebArticle> webArticleList1=new ArrayList<WebArticle>();
                   for(int i=0;i<num;i++){
                       webArticleList1.add(webArticleList.get(i));
                   }
                   webArticleList=webArticleList1;
               }
               webColumnType.setWebArticleList(webArticleList);
           }
        }
        return new ReturnEntity(list);
    }
    /**
     * 统一关于id跳转指定页面
     *
     * @param tagId 标签id
     * @param confId 系统配置id
     * @param fileName 文件名称
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/web/pc/tagType/{confId}")
    public ModelAndView tagType(@PathVariable String confId,String tagId,String fileName, String navId){
        WebConfig webConfig=acceptWebConfig(confId);
        List<WebNavigation> webNavigationList=webNavigationService.acceptList(new WebNavigationVo(BaseConstant.base_use_type_pc,webConfig.getUserId()));
        String url="/pc/"+webConfig.getSiteStyle()+"/defaultTagType";
        if(StrUtil.isNotEmpty(fileName)){
            url="/pc/"+webConfig.getSiteStyle()+"/"+fileName;
        }
        ModelAndView modelAndView=new ModelAndView(url);
        if(StrUtil.isEmpty(navId)){
            if(webNavigationList!=null&&webNavigationList.size()>0){
                modelAndView.addObject("navId",webNavigationList.get(0).getId());
            }
        }else{
            modelAndView.addObject("navId",navId);
        }
        modelAndView.addObject("webNavigationList",webNavigationList);
        modelAndView.addObject("webConfig",webConfig);
        WebTagType webTagType=webTagTypeService.selectByPrimaryKey(tagId);
        modelAndView.addObject("webTagType",webTagType);
        if(webTagType!=null){
            if(WebBaseContent.web_tag_type_lm.equals(webTagType.getTagType())){
                //获取栏目
                List<WebColumnType> list=webColumnTypeService.listLeftByTagId(tagId);
                if(list!=null&&list.size()>0){
                    modelAndView.addObject("colId",list.get(0).getId());
                    modelAndView.addObject("webColumnType",list.get(0));
                }
                modelAndView.addObject("webColumnList",list);
            }else if(WebBaseContent.web_tag_type_wz.equals(webTagType.getTagType())){
                //获取文章
                List<WebArticle> list=webArticleService.listLeftByTagId(tagId);
                if(list!=null&&list.size()>0);{
                    modelAndView.addObject("articleId",list.get(0).getId());
                }
                modelAndView.addObject("articleList",list);
            }
        }
        return modelAndView;
    }
    /**
     * 统一关于标签id跳转指定页面
     *
     * @param colId id
     * @param confId 系统配置id
     * @param fileName 文件名称
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * * */
    @RequestMapping(value="/web/pc/column/{confId}")
    public ModelAndView column(@PathVariable String confId,String colId,String fileName,String navId){
        WebConfig webConfig=acceptWebConfig(confId);
        String url="/pc/"+webConfig.getSiteStyle()+"/defaultTagType";
        if(StrUtil.isNotEmpty(fileName)){
            url="/pc/"+webConfig.getSiteStyle()+"/"+fileName;
        }
        ModelAndView modelAndView=new ModelAndView(url);
        List<WebNavigation> webNavigationList=webNavigationService.acceptList(new WebNavigationVo(BaseConstant.base_use_type_pc,webConfig.getUserId()));
        if(StrUtil.isEmpty(navId)){
            if(webNavigationList!=null&&webNavigationList.size()>0){
                modelAndView.addObject("navId",webNavigationList.get(0).getId());
            }
        }else{
            modelAndView.addObject("navId",navId);
        }
        List<WebArticle> webArticleList=webArticleService.acceptList(new WebArticleVo(colId));
        modelAndView.addObject("webArticleList",webArticleList);
        modelAndView.addObject("webNavigationList",webNavigationList);
        modelAndView.addObject("webConfig",webConfig);
        WebColumnType webColumnType=webColumnTypeService.selectByPrimaryKey(colId);
        modelAndView.addObject("webColumnType",webColumnType);
        modelAndView.addObject("colId",colId);
        return modelAndView;
    }
    /**
     * 统一根据标签获取所属信息
     *
     * @param tagId 标签id
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     */
    @RequestMapping(value = "/web/pc/acceptListByTagId",method = RequestMethod.POST)
    public ReturnEntity acceptListByTagId(String tagId){
        WebTagType webTagType=webTagTypeService.selectByPrimaryKey(tagId);
        if(webTagType!=null){
            if(WebBaseContent.web_tag_type_lm.equals(webTagType.getTagType())){
                //获取栏目
                return new ReturnEntity(webColumnTypeService.listLeftByTagId(tagId));
            }else  if(WebBaseContent.web_tag_type_wz.equals(webTagType.getTagType())){
                //获取文章
                return new ReturnEntity(webArticleService.listLeftByTagId(tagId));
            }else {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,"标签错误");
    }
    /**
     * 分页查询
     *
     * @param webProductVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @RequestMapping(value = "/web/pc/webProduct/page",method = RequestMethod.POST)
    public ReturnEntity page(WebProductVo webProductVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = webProductService.page(webProductVo,pageSize,pageNum);
        return returnEntity;
    }
    /**
     * 分页查询
     *
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @RequestMapping(value = "/web/pc/webArticle/page",method = RequestMethod.POST)
    public ReturnEntity page(WebArticleVo webArticleVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = webArticleService.page(webArticleVo,pageSize,pageNum);
        return returnEntity;
    }
    /**
     * 获取文章信息
     *
     * @param articleId 栏目id
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @RequestMapping(value = "/web/pc/selectArticleDesc",method = RequestMethod.POST)
    public ReturnEntity selectArticleDesc(String articleId){
        return new ReturnEntity(webArticleService.selectByPrimaryKey(articleId));
    }
    /**
     * 获取栏目信息
     *
     * @param colId 栏目id
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    @RequestMapping(value = "/web/pc/selectColDesc",method = RequestMethod.POST)
    public ReturnEntity selectColDesc(String colId){
        return new ReturnEntity(webColumnTypeService.selectByPrimaryKey(colId));
    }
    /**
     * 统一根据栏目获取文档
     *
     * @param userId 账号id
     * @param colId 栏目id
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     */
    @RequestMapping(value = "/web/pc/articleList",method = RequestMethod.POST)
    public ReturnEntity acceptArticle(String userId,String colId,@RequestParam(defaultValue="-1") int num){
        List<WebArticle> webArticleList=webArticleService.acceptList(new WebArticleVo(colId,BaseConstant.base_show_type_zs,userId));
        List<WebArticle> webArticleResultList=new ArrayList<WebArticle>();
        if(num<0){
            return new ReturnEntity(webArticleList);
        }else{
            if(webArticleList!=null&&webArticleList.size()>0){
                int i=0;
                for(WebArticle webArticle:webArticleList){
                    if(i<num){
                        webArticleResultList.add(webArticle);
                    }else{
                        break;
                    }
                    i++;
                }
            }
            return new ReturnEntity(webArticleResultList);
        }

    }
    /**
     * 统一获取移动端指定栏目banner图
     *
     * @param userId 账号id
     * @param colId 栏目id
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     */
    @RequestMapping(value = "/web/pc/banner",method = RequestMethod.POST)
    public ReturnEntity acceptBanner(String userId,String colId){
        List<WebBanner> webBannerList=webBannerService.acceptList(new WebBannerVo(colId, BaseConstant.base_show_type_zs,BaseConstant.base_use_type_pc,userId));
        return new ReturnEntity(webBannerList);
    }


    public WebConfig acceptWebConfig(String xygId){
        if(StrUtil.isEmpty(xygId)){
            xygId="20190606093629083028986917194367";
        }
        return webConfigService.selectByPrimaryKey(xygId);
    }
}
